package hos.ui.edit;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.InputFilter;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.appcompat.widget.AppCompatTextView;

import hos.ui.drawable.BackgroundDrawable;
import hos.ui.R;
import hos.ui.inter.OnClickableListener;

/**
 * <p>Title: IconTextView </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023/6/9 13:59
 */
public class IconTextView extends AppCompatTextView implements View.OnTouchListener,
        BackgroundDrawable<IconTextView>, OnClickableListener {
    /**
     * 后缀图标
     */
    private Drawable mSuffixDrawable;
    /**
     * 前缀图标
     */
    private Drawable mPrefixDrawable;

    private View.OnTouchListener mOnTouchListener;

    private View.OnClickListener mOnSuffixIconClickListener;

    private View.OnClickListener mOnPrefixIconClickListener;

    public IconTextView(Context context) {
        this(context, null);
    }

    public IconTextView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.textViewStyle);
    }

    public IconTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.IconTextView, defStyleAttr, 0);
        try {
            // 处理后缀图标
            int cdId = typedArray.getResourceId(R.styleable.IconTextView_suffixIcon, -1);
            if (cdId == -1) {
                cdId = typedArray.getInt(R.styleable.IconTextView_suffixIcon, -1);
            }
            if (cdId != -1) {
                setSuffixIcon(cdId);
            } else {
                Drawable drawable = typedArray.getDrawable(R.styleable.IconTextView_suffixIcon);
                if (drawable != null) {
                    setSuffixIcon(drawable);
                }
            }
            // 前缀按钮
            int prefixId = typedArray.getResourceId(R.styleable.IconTextView_prefixIcon, -1);
            if (prefixId == -1) {
                prefixId = typedArray.getInt(R.styleable.IconTextView_prefixIcon, -1);
            }
            if (prefixId != -1) {
                setPrefixIcon(prefixId);
            } else {
                Drawable drawable = typedArray.getDrawable(R.styleable.IconTextView_prefixIcon);
                if (drawable != null) {
                    setPrefixIcon(drawable);
                }
            }
            int backgroundType = typedArray.getInteger(R.styleable.IconTextView_backgroundType, -1);
            IconEditText.setBackground(this,backgroundType);
        } finally {
            typedArray.recycle();
        }
        super.setOnTouchListener(this);
    }

    public void setSuffixIcon(Drawable src) {
        mSuffixDrawable = src;
        if (mSuffixDrawable == null) {
            return;
        }
        mSuffixDrawable.setBounds(0, 0, mSuffixDrawable.getIntrinsicWidth(), mSuffixDrawable.getIntrinsicHeight());
        final Drawable[] drawables = getCompoundDrawables();
        setCompoundDrawables(
                drawables[0],
                drawables[1],
                mSuffixDrawable,
                drawables[3]);
    }

    public void setSuffixIcon(int src) {
        setSuffixIcon(getDrawable(getContext(), src));
    }

    public void setPrefixIcon(Drawable src) {
        mPrefixDrawable = src;
        if (mPrefixDrawable == null) {
            return;
        }
        mPrefixDrawable.setBounds(0, 0, mPrefixDrawable.getIntrinsicWidth(), mPrefixDrawable.getIntrinsicHeight());
        final Drawable[] drawables = getCompoundDrawables();
        setCompoundDrawables(
                mPrefixDrawable,
                drawables[1],
                drawables[2],
                drawables[3]);
    }

    public void setPrefixIcon(int src) {
        setPrefixIcon(getDrawable(getContext(), src));
    }

    @Override
    public void setOnTouchListener(final View.OnTouchListener onTouchListener) {
        mOnTouchListener = onTouchListener;
    }

    public void setOnSuffixIconClickListener(OnClickListener iconClickListener) {
        this.mOnSuffixIconClickListener = iconClickListener;
    }

    public void setOnPrefixIconClickListener(OnClickListener iconClickListener) {
        this.mOnPrefixIconClickListener = iconClickListener;
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        final int x = (int) motionEvent.getX();
        if (mSuffixDrawable != null && mSuffixDrawable.isVisible() && x > getWidth() - getPaddingRight() - mSuffixDrawable.getIntrinsicWidth()) {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                if (mOnSuffixIconClickListener != null) {
                    mOnSuffixIconClickListener.onClick(view);
                }
            }
            return true;
        }
        if (mPrefixDrawable != null && mPrefixDrawable.isVisible() && x < mPrefixDrawable.getIntrinsicWidth()) {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                if (mOnPrefixIconClickListener != null) {
                    mOnPrefixIconClickListener.onClick(view);
                }
            }
            return true;
        }
        return mOnTouchListener != null && mOnTouchListener.onTouch(view, motionEvent);
    }

    private Drawable getDrawable(Context context, int id) {
        if (Build.VERSION.SDK_INT >= 21) {
            return context.getDrawable(id);
        }
        return context.getResources().getDrawable(id);
    }


    public void loadLonlat(){
        setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
    }

    public void clickable(boolean isClickable) {
        setClickable(isClickable);
        setEnabled(isClickable);
        setFocusable(isClickable);
    }

    public void clearListener() {
        mOnSuffixIconClickListener = null;
        mOnPrefixIconClickListener = null;
    }
}
